package org.example.controller.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.annotation.OperationLog;
import org.example.controller.LoginController;
import org.example.domain.User;
import org.example.domain.response.ResponseResult;
import org.example.services.impl.LoginServiceImpl;
import org.example.utils.RedisCache;
import org.example.utils.ipAddressSearchUtil.IpAddressSearchWebServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

@Slf4j
@RestController
public class LoginControllerImpl implements LoginController {

//    @GetMapping("/login/test")
//    @PreAuthorize("hasAuthority('system:user:delete')")
//    public ResponseResult test(@RequestBody @Validated(User.Login.class) User user){
//        return ResponseResult.success(null);
//    }

    @GetMapping("/test")
    public void test(){
        String country1 = IpAddressSearchWebServiceClient.getCountryByIp("200.0.0.5");
        System.out.println(country1);
    }

    @Autowired
    private LoginServiceImpl loginService;
    @Autowired
    private RedisCache redisCache;

    //登出
    @Override
    @GetMapping("/logoutNow")
    @OperationLog("登出")
    public ResponseResult logout(HttpServletRequest request){
        return loginService.logout(request);
    }

    //通过用户名登录
    @Override
    @PostMapping("/login/loginByUserName")
    @OperationLog("通过用户名登录")
    public ResponseResult loginByUserName(@RequestBody @Validated(User.Login.class) User user, @RequestParam @NotNull String uuid){
        log.info(user.getUsername() + "通过用户名登录");
        return loginService.loginByUserName(user,uuid);
    }

    //通过用户名注册
    @Override
    @PostMapping("/login/register")
    @OperationLog("用户注册")
    public ResponseResult register(@RequestBody @Validated(User.Register.class) User user,@RequestParam @NotNull String uuid){
        log.info(user.getUsername() + "用户注册");
        return loginService.register(user,uuid);
    }

    //获取图片验证码
    @Override
    @GetMapping("/login/getCheckCode")
    public ResponseResult getCheckCode(HttpServletRequest request){
        log.info("获取验证码");
        return loginService.getCheckCode(request);
    }

    //通过邮箱发送验证码
    @Override
    @GetMapping("/login/getCheckCodeByMail")
    public ResponseResult getCheckCodeByMail(@RequestParam String mail){
        log.info("校验通过，向"+ mail + "邮箱发送验证码");
        return loginService.getCheckCodeByMail(mail);
    }
    //检验邮箱验证码，并登录
    @Override
    @PostMapping("/login/loginByMail")
    @OperationLog("通过邮箱登录")
    public ResponseResult loginByMail(@RequestBody @Validated(User.LoginByMail.class) User user,@RequestParam String uuid){
        log.info("检验" + user.getMail() + "邮箱接受的验证码");
        return loginService.loginByMail(user,uuid);
    }

}
